//
//  GatherViewController.m
//  UGoods
//
//  Created by 宝银 on 16/1/15.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "GatherViewController.h"
#import "GatherModel.h"
#import "BaseTableView.h"
#import "LHAFNetworkTool.h"
#import "GatherTableViewCell.h"
#import "GatherDetailViewController.h"
#import <MJRefresh.h>
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"
@interface GatherViewController ()<UITableViewDelegate, UITableViewDataSource, GatherTableViewCellDelegate>

@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation GatherViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}

- (void)handleData {
    NSString *str = @"http://api.ujipin.com/v3/feed/getnew?channel=appstore&idfa=1EF25F55-6367-4567-B0D5-82DF4F1557CD&user_id=470825&plantform=iphone&version=314&uid=470825&start=0";
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *array = resposeObject[@"data"][@"list"];
        self.dataArray = [GatherModel modelHandleWithArray:array];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"解析失败：%@", error);
    }];
}

- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[GatherTableViewCell class] forCellReuseIdentifier:@"cell"];
}

// 下拉刷新
- (void)addHeaderRefresh {
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        
        [self handleData];
    }];
    // 设置普通状态的动画图片
    NSArray *idleImages = @[[UIImage imageNamed:@"loading_0"]];
    [header setImages:idleImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态
    NSArray *pullingImages = @[[UIImage imageNamed:@"loading_0"]];
    [header setImages:pullingImages forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSInteger i = 0; i < 24; i++) {
        [refreshingImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loading_%ld", i]]];
    }
    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    // 设置所有数据加载完毕的动画图片
    NSArray *NoMoreDataImages = @[[UIImage imageNamed:@"loading_0"]];
    [header setImages:NoMoreDataImages forState:MJRefreshStateNoMoreData];
    
    self.tableView.mj_header = header;
    
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 隐藏状态
    header.stateLabel.hidden = YES;
    
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GatherModel *model = _dataArray[indexPath.row];
    GatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    cell.model = model;
    return cell;
}

// 跳转购买详情页
- (void)pushBuyDetailWithStr:(NSString *)str {
    BuyDetailViewController *buy = [[[BuyDetailViewController alloc] init] autorelease];
    buy.goods_id = [NSString stringWithFormat:@"%@", str];
//    UIViewController *responder = (UIViewController *)self.nextResponder.nextResponder;
//    NSLog(@"%@", responder);
    [self.navigationController pushViewController:buy animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GatherModel *model = _dataArray[indexPath.row];
    CGFloat h = [GatherTableViewCell heightWithText:model.pingjia];
    return h * HEIGHT + 200 * HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GatherDetailViewController *detail = [[GatherDetailViewController alloc] init];
    GatherModel *model = _dataArray[indexPath.row];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"U 集";
    [self handleData];
    [self createTableView];
    [self addHeaderRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

